package com.damon.android.interview.asynctask;

import android.os.AsyncTask;
import android.widget.ProgressBar;
import android.widget.TextView;

/**
 * Created by Damon.Han on 2018/10/18 0018.
 *
 * @author Damon
 */
public class UpdateinfoTask extends AsyncTask<Integer, Integer, String> {
    TextView textView;
    ProgressBar progressBar;

    public UpdateinfoTask(TextView textView, ProgressBar progressBar) {
        this.textView = textView;
        this.progressBar = progressBar;
    }

    @Override
    protected String doInBackground(Integer... integers) {
        int i = 0;
        for (i = 10; i <= 100; i += 10) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            publishProgress(i);
        }
        return integers[0].intValue() + "";
    }

    @Override
    protected void onPreExecute() {
        textView.setText("开始执行异步操作");
    }

    @Override
    protected void onPostExecute(String s) {
        textView.setText("异步操作执行完毕" + s);
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        int val = values[0];
        progressBar.setProgress(val);
        textView.setText("" + val);
    }

    @Override
    protected void onCancelled(String s) {
        super.onCancelled(s);
    }

    @Override
    protected void onCancelled() {
        super.onCancelled();
    }
}
